package vista;

import java.awt.Color;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;


import constants.Constants;
/**
 * this class will create a notification panel with an image to watch context actions of the game 
 * like a new card or a new "misa"  
 * @author Jose Luis Garcia && Alvaro Blazquez Checa
 *
 */
public class ImageMessage extends JDialog implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<Image> cardImages;
	private List<Image> monkImages;
	private List<Image> misaImages;
	private JLabel imageLabel;
	private JTextArea messageText;
	private JButton exitButton;
	private JTextArea cardTextArea,cardTitleArea;
	private int cardType = -1;
	private String cardName;
	
	/**
	 * this construct will instanciate the panel 
	 * @param bounds are the size for the panel
	 * @param cardImagesParam all of the posible cards to show of this type
	 * @param monkImagesParam all of the posible cards to show of this type
	 * @param misaImagesParam all of the posible cards to show of this type
	 */
	public ImageMessage(Rectangle bounds,List<Image> cardImagesParam,List<Image> monkImagesParam,List<Image> misaImagesParam) {
		
		this.setBounds(bounds);
		this.setLayout(null);
		this.setModal(true);
		
		this.cardImages = new ArrayList<Image>();
		this.monkImages = new ArrayList<Image>();		
		this.misaImages = new ArrayList<Image>();
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(Constants.doubleBorder);
		panel.setBackground(Color.DARK_GRAY);
		setContentPane(panel);
		
		exitButton = new JButton("X");
		exitButton.addActionListener(this);
		exitButton.setBackground(Color.BLACK);
		exitButton.setForeground(Color.WHITE);
		exitButton.setFont(Constants.NORMAL_FONT);
		exitButton.setBounds(90, 377, 70, 18);
		panel.add(exitButton);
		
		cardTextArea = new JTextArea();
		cardTextArea.setBounds(25, 145, 200, 215);
		cardTextArea.setOpaque(false);
		cardTextArea.setLineWrap(true);
		cardTextArea.setWrapStyleWord(true);
		cardTextArea.setEditable(false);
		cardTextArea.setForeground(Color.WHITE);
		cardTextArea.setFont(Constants.NORMAL_FONT);
		panel.add(cardTextArea);
		
		cardTitleArea = new JTextArea();
		cardTitleArea.setBounds(25, 90, 150, 60);
		cardTitleArea.setOpaque(false);
		cardTitleArea.setLineWrap(true);
		cardTitleArea.setWrapStyleWord(true);
		cardTitleArea.setEditable(false);
		cardTitleArea.setForeground(Color.RED);
		cardTitleArea.setFont(Constants.BIG_FONT);
		panel.add(cardTitleArea);
		
		imageLabel = new JLabel();
		imageLabel.setBounds(5, 50, getWidth()-10, getHeight()-75);
		imageLabel.setForeground(Color.WHITE);
		imageLabel.setFont(Constants.NORMAL_FONT);
		panel.add(imageLabel);
		
		messageText = new JTextArea();
		messageText.setBounds(10, 10, getWidth()-20, getHeight()-imageLabel.getHeight()-40);
		messageText.setLineWrap(true);
		messageText.setWrapStyleWord(true);
		messageText.setForeground(Color.WHITE);
		messageText.setFont(Constants.NORMAL_FONT);
		messageText.setAlignmentX(CENTER_ALIGNMENT);
		messageText.setBorder(null);
		messageText.setEditable(false);
		messageText.setOpaque(false);
		panel.add(messageText);
		
		scaleImages(cardImagesParam,monkImagesParam,misaImagesParam);
		setUndecorated(true);
		setAlwaysOnTop(true);
		setVisible(false);
	}
	
	/**
	 * this method returns a cardType
	 * @return cardType
	 */	
	public int getCardType() {
		return cardType;
	}
	/**
	 * this method returns a cardName
	 * @return cardName
	 */
	public String getCardName() {
		return cardName;
	}

/**
 * this method instantiate the panel with the posible params  
 * @param cardType if you want to show a cardType
 * @param image relative to the cardType
 * @param message to show in the upper panel zone
 * @param cardTitle to show in the card zone
 * @param cardText to show in the card zone
 */
	public void showMessage(int cardType,int image,String message,String cardTitle ,String cardText) {
		
		if(cardType == Constants.MONK_CARD_TYPE && ((0 <= image) && (image<= monkImages.size()-1))) {
			this.cardType = cardType;
			cardTextArea.setVisible(false);
			cardTitleArea.setVisible(false);
			imageLabel.setIcon(new ImageIcon(monkImages.get(image)));
			messageText.setText(message);
			setVisible(true);
		}
		else if(cardType == Constants.PLAY_CARD_TYPE && ((0 <= image) && (image<= cardImages.size()-1)) && cardTitle!=null && cardText!=null) {
			this.cardType = cardType;
			this.cardName = cardTitle;
			cardTitleArea.setVisible(true);
			cardTextArea.setVisible(true);
			imageLabel.setIcon(new ImageIcon(cardImages.get(image)));
			cardTitleArea.setText(cardTitle);
			cardTextArea.setText(cardText);
			messageText.setText(message);
			setVisible(true);
		}
		else if(cardType == Constants.MISA_CARD_TYPE  && ((0 <= image) && (image<= cardImages.size()-1))) {
			this.cardType = cardType;
			cardTextArea.setVisible(false);
			cardTitleArea.setVisible(false);
			imageLabel.setIcon(new ImageIcon(misaImages.get(image)));
			messageText.setText(message);
			setVisible(true);
		}
	}
	/**
	 * this is created to hide the panel when buttons were pressed
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		
		setVisible(false);
		
	}
	
	/**
	 * this method will change the size of our images to make them change to the size of 
	 * the card for been added to the panel
	 * @param cardImagesParam the cardImages to be added
	 * @param monkImagesParam the monkImages to be added
	 * @param misaImagesParam the misaImages to be added
	 */
	private void scaleImages(final List<Image> cardImagesParam,final List<Image> monkImagesParam,final List<Image> misaImagesParam) {
		
		if(cardImagesParam!=null) {
			for(Image image:cardImagesParam) {
				this.cardImages.add(image.getScaledInstance(imageLabel.getWidth(), imageLabel.getHeight(), BufferedImage.SCALE_SMOOTH));
			}
		}
		if(monkImagesParam!=null) {
			for(Image image:monkImagesParam) {
				this.monkImages.add(image.getScaledInstance(imageLabel.getWidth(), imageLabel.getHeight(), BufferedImage.SCALE_SMOOTH));
			}
		}
		if(misaImagesParam!=null) {
			for(Image image:misaImagesParam) {
				this.misaImages.add(image.getScaledInstance(imageLabel.getWidth(), imageLabel.getHeight(), BufferedImage.SCALE_SMOOTH));
			}
		}
	}
}
